package com.cenwangkeji.resident.ui.activity.special;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.ChBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.SpecialDiseaseDetailAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;
import com.lws.basic.view.adapter.AdapterIntegerDefault0;
import com.lws.basic.view.adapter.DoubleDefault0Adapter;

import com.tencent.bugly.crashreport.CrashReport;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class ChanHouDetailActivity extends BaseActivity {


    @Override
    public int setLayoutId() {
        return R.layout.activity_xer_detail;
    }

    @BindView(R.id.rv)
    RecyclerView mRv;
    private SpecialDiseaseDetailAdapter mAdapter;

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
        getData();
    }

    private void initRecycelrView() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialDiseaseDetailAdapter(null);
        mRv.setAdapter(mAdapter);
    }


    private void getData() {
        showLoading();
        Observable.create(new ObservableOnSubscribe<ChBean>() {
            @Override
            public void subscribe(ObservableEmitter<ChBean> emitter) throws Exception {
                int id = getIntent().getIntExtra("id", 0);
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 3);
                map.put("zxbId", id);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkHttpUtils.post( jsonObject.toString(),Constant.ch_detail);
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json,ChBean.class));
            }
        }).compose(RxUtils.<ChBean>io_main()).subscribe(new ObserverOnNext<ChBean>() {
            @Override
            public void onNext(ChBean bean) {
                try {
                    dismissLoading();
                    if (bean.getData() != null) {
                        List<String> list = new ArrayList<>();
                        list.add("分娩日期:  " + bean.getData().getFwrq());
                        list.add("出院日期:  " + bean.getData().getCyrq());
                        list.add("体温（℃）:  " + bean.getData().getTw());
                        list.add("一般健康情况:  " + bean.getData().getYbjkzk());
                        list.add("一般心理情况:  " + bean.getData().getYbxlzk());
                        list.add("血压收缩率:  " + bean.getData().getXySsl());
                        list.add("血压舒张率:  " + bean.getData().getXySzl());
                        list.add("乳房:  " + (bean.getData().getChRf() == 2 ? "异常" : "未见异常"));
                        list.add("恶露:  " + (bean.getData().getChEl() == 2 ? "异常" : "未见异常"));
                        list.add("子宫:  " + (bean.getData().getChZg() == 2 ? "异常" : "未见异常"));
                        list.add("伤口:  " + (bean.getData().getChSk() == 2 ? "异常" : "未见异常"));
                        list.add("其他:  " + bean.getData().getChQt());
                        list.add("分类:  " + (bean.getData().getChFl() == 2 ? "异常" : "未见异常"));
                        list.add("指导:  \n" +
                                zd(bean.getData().getZdList(), bean.getData().getZdQt()));
                        if (bean.getData().getZzjy() == 2) {// 1无 2有
                            list.add("转诊原因：" + bean.getData().getZzyy());
                            list.add("机构科室：" + bean.getData().getZzjg()
                                    + "    " + bean.getData().getZzks());
                        }

                        list.add("时间：" + bean.getData().getCreateDate());
                        mAdapter.setNewData(list);
                    } else {
                        MyToast.show(bean.getMsg());
                    }
                }catch (Exception e){
                    MyToast.show(e.getMessage());
                    CrashReport.postCatchedException(e);
                }
            }
        });

    }

    //指导
    private String zd(List<String> list ,String qt){
        String s ="";
        for (int i =0; i<list.size() ;i++) {
            switch (list.get(i)) {
                case "1":
                    s += "个人卫生  ";
                    break;
                case "2":
                    s += "心理  ";
                    break;
                case "3":
                    s += "营养  ";
                    break;
                case "4":
                    s += "母乳喂养  ";
                    break;
                case "5":
                    s += "新生儿护理与喂养   ";
                    break;
                case "6":
                    s +=qt;
                    break;


            }
        }
        return s;

    }

}
